mini.parse();

function CommentsHandler() {
};

var kst = "";

try {
	CommentsHandler.commentsGrid = mini.get("commentsGrid");
	CommentsHandler.commentsGrid.load();
} catch (e) {
}

var databucType = [ {
	id : "-1",
	text : "全部"
}, {
	id : "0",
	text : "商品"
}, {
	id : "1",
	text : "文章"
} ];

var databucState = [ {
	id : "-1",
	text : "全部"
}, {
	id : "0",
	text : "隐藏"
}, {
	id : "1",
	text : "展示"
} ];

try {
	var bucType = mini.get("bucType");
	bucType.load(databucType);
	bucType.setValue("-1");
} catch (e) {
}

try {
	var bucState = mini.get("bucState");
	bucState.load(databucState);
	bucState.setValue("-1");
} catch (e) {
}

CommentsHandler.isQueryBtnExpended = false;
CommentsHandler.queryShow = function() {
	if (CommentsHandler.isQueryBtnExpended) {
		CommentsHandler.isQueryBtnExpended = false;
		mini.get("member_comments_query").setText("展开查询");
		mini.get("member_comments_query").setIconCls("icon-btn-expand");
	} else {
		CommentsHandler.isQueryBtnExpended = true;
		mini.get("member_comments_query").setText("收起查询");
		mini.get("member_comments_query").setIconCls("icon-btn-collapse");
	}
	$("#queryCommentsDiv").toggle(500);
	setTimeout(function() {
		mini.get("commentsPanel").doLayout();
	}, 600);
};

/**
 * 根据用户权限展示功能按钮
 */
CommentsHandler.loadToolbar = function() {
	var array = new Array("member_comments_del","member_comments_query","member_comments_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
CommentsHandler.loadToolbar();

CommentsHandler.delComments = function() {
	var rowsData = CommentsHandler.commentsGrid.getSelecteds();
	
	if (rowsData.length >= 1) {
		var str = "";
		
		for ( var i = 0; i < rowsData.length; i++) {
			str += rowsData[i].buc_id + ",";
		}
		
		mini.confirm("确定删除选择的"+rowsData.length+"条记录？", "确定？",
            function (action) {            
                if (action == "ok") {
                	CommentsHandler.commentsGrid.loading();
                	
                    $.ajax( {
						url : __rootUrl + "delComments.do",
						type : "POST",
						data : {
							bucId : str.substring(0, str.length-1),
							lslUser: gbOperateUser,
    						lslAddress: gbOperateIp
						},
						cache : false,
						success : function(text) {
							MiniuiTools.miniInfo("系统提示","操作成功！", function(){
				        		CommentsHandler.commentsGrid.reload();
				        	});
						},
						error : function(jqXHR, textStatus, errorThrown) {
							mini.alert(jqXHR.responseText, "系统提示", null);
						},
						complete: function(){
							CommentsHandler.commentsGrid.unmask();
						}
					});
                } 
            }
        );
		
	} else {
		mini.alert("请选择删除的记录", "系统提示", null);
	}
}

CommentsHandler.doQuery = function() {
	var bucName = mini.get("bucName").getValue();
	var bucType = mini.get("bucType").getValue();
	var sbucTime = mini.get("sbucTime").getValue() != null ? mini.get(
			"sbucTime").getValue().format("yyyy-MM-dd hh:mm") : "";
	var ebucTime = mini.get("ebucTime").getValue() != null ? mini.get(
			"ebucTime").getValue().format("yyyy-MM-dd hh:mm") : "";
	var bucState = mini.get("bucState").getValue();
	
	CommentsHandler.commentsGrid.load( {
		bucName : bucName,
		bucType : bucType,
		sbucTime : sbucTime,
		ebucTime : ebucTime,
		bucState : bucState
	});
}

/**
 * 查找用户评论
 */
CommentsHandler.findComment = function() {
	if (!MiniuiTools.hasPurview("member_comments_reply")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = CommentsHandler.commentsGrid.getSelected();
	var rowsData = CommentsHandler.commentsGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		var x = [ "商品", "文章" ];
		var commentsTitle;
		
		if (rowData.buc_type.toString() != "") {
			commentsTitle = x[parseInt(rowData.buc_type)]+"评论信息";
		} else {
			commentsTitle = "";
		}
		
		miniWin.setParams(550, 450, commentsTitle);
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.load("memberCommentsList.jsp",function(){
			var iframe = miniWin.getIFrameEl();
			//通过传递ID查询获取数据
//          var data = {action: "edit", id: rowData.buc_id};
//          iframe.contentWindow.CommentsFormHandler.passData(data);
			//通过直接获取该列数据传递数据
			iframe.contentWindow.CommentsFormHandler.passData(rowData);
		},function(action){
			CommentsHandler.commentsGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
}

CommentsHandler.changeState = function (state){
	//权限控制代码
	if (!MiniuiTools.hasPurview("member_comments_state")) {
		return;
	}
	
	var rowData = CommentsHandler.commentsGrid.getSelected();
	
	$.ajax( {
		url : __rootUrl + "auditComments.do",
		type : "POST",
		data : {
			bucId : rowData.buc_id,
			bumState : state,
			lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
		},
		cache : false,
		success : function(text) {
			CommentsHandler.commentsGrid.reload();
		},
		error : function(jqXHR, textStatus, errorThrown) {
			mini.alert(jqXHR.responseText, "系统提示", null);
		}
	});
}

try{
	CommentsHandler.commentsGrid.on("drawcell",
		function(e) {
			var record = e.record;
			var column = e.column;
			var field = e.field;
			var value = e.value;
			var x = [ "商品", "文章" ];
			var y = [ "隐藏", "展示" ];
			
			if (column.field == "buc_type") {
				if (e.value.toString() != "") {
					e.cellHtml = x[parseInt(e.value)];
				} else {
					e.cellHtml = "";
				}
			}
			
			if (column.field == "buc_state") {
				if (e.value.toString() != "") {
					e.cellHtml = y[parseInt(e.value)];
				} else {
					e.cellHtml = "";
				}
			}
			
			if (column.field == "buc_time") {
				if (e.value.toString() != "") {
					e.cellHtml = changeDate(e.value,6);
				} else {
					e.cellHtml = "";
				}
			}
			
			if (column.field == "buc_id") {
				if (e.value.toString() != "") {
					var ks="";
					if(record.bri_id!=null&&""!=record.bri_id)
						ks=record.bri_id;
					e.cellHtml = '<a class="New_Button" href="javascript:void(0);"  onclick="CommentsHandler.findComment();">回复</a>';
				} else {
					e.cellHtml = "查看";
				}
			}
			
			if (column.field == "buc_target") { 
				if (e.value.toString() != "") {
//							e.cellHtml = '<a href="findShop.jsp?id=' + e.value+ '">' + record.buc_title + '</a>';
					e.cellHtml = '<a href="#">' + record.buc_title + '</a>';
				} else {
					e.cellHtml = "";
				}
			}
			
			if (column.field == "buc_state") {
		        if (e.value == 1) {
		            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='CommentsHandler.changeState(0)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
		        } else {
		            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='CommentsHandler.changeState(1)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
		        }
			}
		});
}catch(e){
}

/**
 * 刷新页面数据及布局
 */
CommentsHandler.afresh = function(){
	CommentsHandler.doQuery();
	mini.get("commentsPanel").doLayout();
}

/**
 * 关闭窗口
 * 
 * @param {}
 */
CommentsHandler.closeCurrentWin = function(action) {
	if (window.CloseOwnerWindow)
		window.CloseOwnerWindow(action);
	else
		window.close();
};
